import requests
import execjs


def get_lang(query):
    # 获取输入的语言信息
    cookies = {
        '__yjs_duid': '1_140123a02c6bed03fec49c4114d223e01644818397909',
        'REALTIME_TRANS_SWITCH': '1',
        'HISTORY_SWITCH': '1',
        'FANYI_WORD_SWITCH': '1',
        'SOUND_SPD_SWITCH': '1',
        'SOUND_PREFER_SWITCH': '1',
        'APPGUIDE_10_0_2': '1',
        'BIDUPSID': 'E5882F3B57C0566717B02B36636FC8F6',
        'PSTM': '1645010945',
        'BAIDU_WISE_UID': 'wapp_1648449251145_801',
        'BAIDUID_BFESS': 'B4F21E5D0635872A813D7AB870D1D23D:FG=1',
        'BAIDUID': 'E5882F3B57C0566717B02B36636FC8F6:SL=0:NR=10:FG=1',
        'BDUSS': 'h-Y2NIZUJ6MlFLQlZESGNwcWREeS01RTh-VktteFlJVzNCYnVNWDFGTWhQMjlpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGyR2IhskdiYj',
        'BDUSS_BFESS': 'h-Y2NIZUJ6MlFLQlZESGNwcWREeS01RTh-VktteFlJVzNCYnVNWDFGTWhQMjlpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGyR2IhskdiYj',
        '__xsptplus861': '861.2.1648866221.1648866261.5%234%7C%7C%7C%7C%7C%23%23LKzW72gUu6dB3ZPPWyyuumwwI1-N--IO%23',
        'RT': '"z=1&dm=baidu.com&si=4paw8dht8x4&ss=l1h84xau&sl=p&tt=9jq&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=jcyv&cl=1uwk&ul=k4gy&hd=k4h9"',
        'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574': '1648700795,1648792569,1648822952,1648867388',
        'Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574': '1648867388',
        'ab_sr': '1.0.1_MjIwZjU5MmZiMTU0ZDk2ZTgxNTg5M2M1Mzk1NGEwNzBiNjAxNDkxMjI3NTRkZDkyODBlNTQ3NTY5ZTA5MmZmN2E4MDkwZTM4NjEyNjI5YTg5NDkzODUwMjdmNmQ5YTUzMWVhN2Y3ZmVjYTY4Zjg3YzBjOTIzYWViZjFhZWM4ODBiOWIzOGUwMWVlZjRhYzI5YTg1NDdjMjYzYTRjNjIyNzE0YmM0MjFhMjU0M2Y0MjM1MTcyMWU2YjRjMDYzNjYx',
    }
    headers = {
        'Connection': 'keep-alive',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Microsoft Edge";v="99"',
        'Accept': '*/*',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'X-Requested-With': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
        'sec-ch-ua-platform': '"Windows"',
        'Origin': 'https://fanyi.baidu.com',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://fanyi.baidu.com/',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        # Requests sorts cookies= alphabetically
        # 'Cookie': '__yjs_duid=1_140123a02c6bed03fec49c4114d223e01644818397909; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; APPGUIDE_10_0_2=1; BIDUPSID=E5882F3B57C0566717B02B36636FC8F6; PSTM=1645010945; BAIDU_WISE_UID=wapp_1648449251145_801; BAIDUID_BFESS=B4F21E5D0635872A813D7AB870D1D23D:FG=1; BAIDUID=E5882F3B57C0566717B02B36636FC8F6:SL=0:NR=10:FG=1; BDUSS=h-Y2NIZUJ6MlFLQlZESGNwcWREeS01RTh-VktteFlJVzNCYnVNWDFGTWhQMjlpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGyR2IhskdiYj; BDUSS_BFESS=h-Y2NIZUJ6MlFLQlZESGNwcWREeS01RTh-VktteFlJVzNCYnVNWDFGTWhQMjlpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGyR2IhskdiYj; __xsptplus861=861.2.1648866221.1648866261.5%234%7C%7C%7C%7C%7C%23%23LKzW72gUu6dB3ZPPWyyuumwwI1-N--IO%23; RT="z=1&dm=baidu.com&si=4paw8dht8x4&ss=l1h84xau&sl=p&tt=9jq&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=jcyv&cl=1uwk&ul=k4gy&hd=k4h9"; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1648700795,1648792569,1648822952,1648867388; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1648867388; ab_sr=1.0.1_MjIwZjU5MmZiMTU0ZDk2ZTgxNTg5M2M1Mzk1NGEwNzBiNjAxNDkxMjI3NTRkZDkyODBlNTQ3NTY5ZTA5MmZmN2E4MDkwZTM4NjEyNjI5YTg5NDkzODUwMjdmNmQ5YTUzMWVhN2Y3ZmVjYTY4Zjg3YzBjOTIzYWViZjFhZWM4ODBiOWIzOGUwMWVlZjRhYzI5YTg1NDdjMjYzYTRjNjIyNzE0YmM0MjFhMjU0M2Y0MjM1MTcyMWU2YjRjMDYzNjYx',
    }
    data = {
        'query': query,
    }
    response = requests.post('https://fanyi.baidu.com/langdetect', headers=headers, cookies=cookies, data=data)
    lang = response.json()['lan']
    params = {}
    if lang == "zh":
        params = {'from':lang, 'to':'en'}
    elif lang == "en":
        params = {'from':lang, 'to':'zh'}
    return params


def get_translant_msg(query, sign):
    # 获取翻译结果
    global data, response
    cookies = {
        '__yjs_duid': '1_140123a02c6bed03fec49c4114d223e01644818397909',
        'REALTIME_TRANS_SWITCH': '1',
        'HISTORY_SWITCH': '1',
        'FANYI_WORD_SWITCH': '1',
        'SOUND_PREFER_SWITCH': '1',
        'SOUND_SPD_SWITCH': '1',
        'APPGUIDE_10_0_2': '1',
        'BIDUPSID': 'E5882F3B57C0566717B02B36636FC8F6',
        'PSTM': '1645010945',
        'BDUSS': '0JjTFVxeVg3MDJIVldkOXZuc0ZiSEdLWkVoZFRoVC01U1plZGlrcmF1cGE0MmhpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFpWQWJaVkFid',
        'BDUSS_BFESS': '0JjTFVxeVg3MDJIVldkOXZuc0ZiSEdLWkVoZFRoVC01U1plZGlrcmF1cGE0MmhpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFpWQWJaVkFid',
        '__xsptplus861': '861.1.1648449067.1648449108.2%234%7C%7C%7C%7C%7C%23%23CRdao4jF3XJr6i2W6UAsXLQoYqj16CDS%23',
        'BAIDU_WISE_UID': 'wapp_1648449251145_801',
        'BAIDUID_BFESS': 'B4F21E5D0635872A813D7AB870D1D23D:FG=1',
        'RT': '"z=1&dm=baidu.com&si=pss5kxij7tk&ss=l1ac1lut&sl=8&tt=880&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=avk9&ul=clwb&hd=clwz"',
        'BAIDUID': 'E5882F3B57C0566717B02B36636FC8F6:SL=0:NR=10:FG=1',
        'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574': '1648602062,1648700795,1648792569,1648822952',
        'Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574': '1648822952',
        'ab_sr': '1.0.1_MjBjYjQxZWNkM2JlZTM0YTk1NzZjNDc3MTA4NGYzYzBmNjhhYTM0MDEyOGMxZGQ5Yjk2YmExY2I1NDFlYmFjZDM5ZDg1YzMwZjI2NWY0YmE4M2EyZjIyNjQxODJiNzBlMGNiNWJlOTgzYmIyZTdhN2M3N2EzMzZmZGFmN2I3ZWMxZmQ3OTc3M2NmYWJhNGEzODBhNjNkMGJhNTJmMTJiY2U2ZWE2NGNmZGRhM2YyZmU4Nzg4YWIzMWFjMmZhYWI3',
    }
    headers = {
        'Connection': 'keep-alive',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Microsoft Edge";v="99"',
        'Accept': '*/*',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'X-Requested-With': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
        'sec-ch-ua-platform': '"Windows"',
        'Origin': 'https://fanyi.baidu.com',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://fanyi.baidu.com/',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        # Requests sorts cookies= alphabetically
        # 'Cookie': '__yjs_duid=1_140123a02c6bed03fec49c4114d223e01644818397909; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; APPGUIDE_10_0_2=1; BIDUPSID=E5882F3B57C0566717B02B36636FC8F6; PSTM=1645010945; BDUSS=0JjTFVxeVg3MDJIVldkOXZuc0ZiSEdLWkVoZFRoVC01U1plZGlrcmF1cGE0MmhpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFpWQWJaVkFid; BDUSS_BFESS=0JjTFVxeVg3MDJIVldkOXZuc0ZiSEdLWkVoZFRoVC01U1plZGlrcmF1cGE0MmhpSUFBQUFBJCQAAAAAAAAAAAEAAABKHFWxSGFydXRhc2F2ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFpWQWJaVkFid; __xsptplus861=861.1.1648449067.1648449108.2%234%7C%7C%7C%7C%7C%23%23CRdao4jF3XJr6i2W6UAsXLQoYqj16CDS%23; BAIDU_WISE_UID=wapp_1648449251145_801; BAIDUID_BFESS=B4F21E5D0635872A813D7AB870D1D23D:FG=1; RT="z=1&dm=baidu.com&si=pss5kxij7tk&ss=l1ac1lut&sl=8&tt=880&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=avk9&ul=clwb&hd=clwz"; BAIDUID=E5882F3B57C0566717B02B36636FC8F6:SL=0:NR=10:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1648602062,1648700795,1648792569,1648822952; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1648822952; ab_sr=1.0.1_MjBjYjQxZWNkM2JlZTM0YTk1NzZjNDc3MTA4NGYzYzBmNjhhYTM0MDEyOGMxZGQ5Yjk2YmExY2I1NDFlYmFjZDM5ZDg1YzMwZjI2NWY0YmE4M2EyZjIyNjQxODJiNzBlMGNiNWJlOTgzYmIyZTdhN2M3N2EzMzZmZGFmN2I3ZWMxZmQ3OTc3M2NmYWJhNGEzODBhNjNkMGJhNTJmMTJiY2U2ZWE2NGNmZGRhM2YyZmU4Nzg4YWIzMWFjMmZhYWI3',
    }
    data = {
        'from': 'en',
        'to': 'zh',
        'query': 'hello',
        'transtype': 'translang',
        'simple_means_flag': '3',
        'sign': '54706.276099',
        'token': '61b23b23667cdee479be8a0b6178329a',
        'domain': 'common',
    }

    params = get_lang(query)
    data['query'] = query
    data['sign'] = sign
    data['from'] = params['from']
    data['to'] = params['to']
    response = requests.post('https://fanyi.baidu.com/v2transapi', headers=headers, params=params, cookies=cookies,
                             data=data)
    msg = response.json()['trans_result']['data'][0]['dst']
    return msg


def get_msg(query):
    with open('x.js', 'r') as f:
        jsshell = execjs.compile(f.read())
    sign = jsshell.call('e', query)
    return get_translant_msg(query, sign)


if __name__ == '__main__':
    print("-----翻译-----")
    while 1:
        query = input('请输入(q退出):')
        if query.lower() != 'q':
            print(get_msg(query))
            continue
        break

